import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'


Vue.use(VueRouter)
//设置push和replace对同一个路由进行多次点击多次跳转
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}

const routes = [
  //首页
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{deep:1,title:'',index:0}
  },
  //搜索
  {
    path: '/search',
    name: 'Search',
    component: () => import('../views/Search.vue'),
    meta:{deep:1,title:'搜索',index:1}
  },
  //订单列表
  {
    path:'/order',
    name:'Order',
    component:()=>import('../views/Order.vue'),
    meta:{deep:1,title:'订单列表',index:2}
  },
  //我的
  {
    path:'/mine',
    name:'Mine',
    component:()=>import('../views/Mine.vue'),
    meta:{deep:1,title:'我的',index:3}
  },
  //登录
  {
    path:'/login',
    name:'Login',
    component:()=>import('../views/Login.vue'),
    meta:{deep:2,title:'登录'}
  },
  //注册
  {
    path:'/regist',
    name:'Regist',
    component:()=>import('../views/Regist.vue'),
    meta:{deep:2,title:'注册'}
  },
  //城市
  {
    path:'/',
    name:'City',
    component:()=>import('../views/City.vue'),
    meta:{deep:3,index:0}
  },
  //城市搜索
  {
    path:'/searchCity',
    name:'SearchCity',
    component:()=>import('../views/SearchCity.vue'),
    meta:{deep:2,title:''}
  },
  // 商品分类列表
  {
    path:'/businessKind',
    name:'BusinessKind',
    component:()=>import('../views/BusinessKinds_f.vue'),
    meta:{deep:2,title:''}
  },
  // 购物页面
  {
    path:'/shop',
    name:'Shop',
    component:()=>import('../views/Shop_f.vue'),
    meta:{deep:0,title:''},
    // children:[
  },
  {
    path:"/goods",  //商品
    name:"Goods",
    component:()=>import('../components/Goods_f.vue'),
    meta:{deep:0,}
  },
  {
    path:"/comment", //评价
    name:"Comment",
    component:()=>import('../components/Comment_f.vue'),
    meta:{deep:0,}
  },
    // ],
    // redirect:{name:'Goods',params:{id:0}},
  // 商家详情
  {
    path:'/shopDetail',
    name:'ShopDetail',
    component:()=>import('../views/ShopDetail_f.vue'),
    meta:{deep:2,title:''}
  },
  // 食品监督安全公示
  {
    path:'/publiciity',
    name:'Publicity',
    component:()=>import('../views/Publicity_f.vue'),
    meta:{deep:2,title:'食品监督安全公示'}
  },
  //确认订单
  {
    path:'/confirmOrder',
    name:'Confirm',
    component:()=>import('../views/ConfirmOrder_S.vue'),
    meta:{deep:2,title:'确认订单',}
  },
  //订单备注
  {
    path:'/orderRemarks',
    name:'OrderRemarks',
    component:()=>import('../views/OrderRemarks_S.vue'),
    meta:{deep:2,title:'订单备注'}
  },
  //选择地址
  {
    path:'/chooseAddress',
    name:'ChooseAddress',
    component:()=>import('../views/ChooseAddress_S.vue'),
    meta:{deep:2,title:'选择地址'}
  },
  //添加地址
  {
    path:'/addAddress',
    name:'AddAddress',
    component:()=>import('../views/AddAddress_S.vue'),
    meta:{deep:2,title:'添加地址'}
  },
  //搜索地址
  {
    path:'/searchAddress',
    name:'SearchAddress',
    component:()=>import('../views/SearchAddress_S.vue'),
    meta:{deep:2,title:'搜索'}
  },
  //服务中心
  {
    path:'/serviceCenter',
    name:'ServiceCenter',
    component:()=>import('../views/ServiceCenter_S.vue'),
    meta:{deep:2,title:'服务中心'}
  },
  //服务中心答案
  {
    path:'/answer',
    name:'Answer',
    component:()=>import('../views/Answer_S.vue'),
    meta:{deep:2,title:''}
  },
  //我的余额
  {
    path:'/accountBalance',
    name:'AccountBalance',
    component:()=>import('../views/AccountBalance_S.vue'),
    meta:{deep:2,title:'我的余额'}
  },
  //我的积分
  {
    path:'/credit',
    name:'Credit',
    component:()=>import('../views/Credit_S.vue'),
    meta:{deep:2,title:'我的积分'}
  },
  //会员中心
  {
    path:'/vip',
    name:'Vip',
    component:()=>import('../views/VipCenter_S.vue'),
    meta:{deep:2,title:'会员中心'}
  },
  //我的优惠
  {
    path:'/discount',
    name:'Discount',
    component:()=>import('../views/Discount_S.vue'),
    meta:{deep:2,title:'我的优惠'},
    children:[{
      path:"dis/:id",
      name:'Dis',
      component:()=>import('../components/Dis_S.vue'),
      meta:{deep:2,title:'我的优惠'}
    },{
      path:"dis/:id",
      name:'Dai',
      component:()=>import('../components/Daijinquan_S.vue'),
      meta:{deep:2,title:'我的优惠'}
    }
  ],
    redirect:{name:'Dis',params:{id:0}}
  },
  //历史红包
  {
    path:'/overdue',
    name:'Over',
    component:()=>import('../views/OverDue_S.vue'),
    meta:{deep:2,title:'历史红包'}
  },
  //兑换红包
  {
    path:'/duihuan',
    name:'DuiHuan',
    component:()=>import('../views/DuiHuan_S.vue'),
    meta:{deep:2,title:'兑换红包'}
  },
  //推荐有奖
  {
    path:'/recommend',
    name:'Recommend',
    component:()=>import('../views/Recommend_S.vue'),
    meta:{deep:2,title:'推荐有奖'}
  },
  //账户信息
  {
    path:'/userinfor',
    name:'UserInfor',
    component:()=>import('../views/UserInfor_S.vue'),
    meta:{deep:2,title:'账户信息'}
  },
  //积分商城
  {
    path:'/pointsMall',
    name:'PointsMall',
    component:()=>import('../views/PointsMall_S.vue'),
    meta:{deep:2,title:'积分商城'}
  },
  //下载饿了么
  {
    path:'/dowload',
    name:'Dowload',
    component:()=>import('../views/Dowload_S.vue'),
    meta:{deep:2,title:'下载'}
  },
  //在线支付
  {
    path:'/onlinePay',
    name:'OnlinePay',
    component:()=>import('../views/OnlinePay_S.vue'),
    meta:{deep:2,title:'在线支付'}
  },
  //支付宝支付
  {
    path:'/zhifubao',
    name:'Zhifu',
    component:()=>import('../views/Zhifubao_S.vue'),
    meta:{deep:2,title:'支付'}
  },
  //重置密码
  {
    path:'/updatePassword',
    name:'Update',
    component:()=>import('../views/UpdatePassword_S.vue'),
    meta:{deep:2,title:'重置密码'}
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//进行全局导航守卫的注册
router.beforeEach(function(to,from,next){
  if(to.name != 'Home' && to.name != 'Mine' && to.name != 'Search'&& to.name != 'Login'&& to.name != 'Regist'&& to.name != 'City'&& to.name != 'SearchCity'&& to.name != 'BusinessKind'&& to.name != 'Shop'&& to.name != 'Goods'&& to.name != 'Comment'&& to.name != 'ShopDetail'&& to.name != 'Publicity'&& to.name != 'ServiceCenter'&& to.name != 'Dowload'&& to.name != 'Answer'&&to.name!='Confirm'){
    if(localStorage.getItem('code') != 200){
      router.replace({name:'Login'});
    }else{
      next();
    }
  }else{
    next();
  }
})

export default router
